/* Copyright 2008. All rights reserved. */
package org.bling.parser.tags;

import org.bling.parser.BlingParserException;
import org.bling.parser.style.StyleParser;
import org.bling.process.events.EventProcessor;
import org.bling.util.Util;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Widget;

/**
 * renders an SWT Button
 * 
 * @author DISBROB
 * 
 */
public class ButtonTag extends Tag {

	protected Button button;
	protected String onSelect = "";
	protected String style;

	public void setStyle(String style) {
		this.style = style;
	}
	
	public void setOnSelect(String onSelect) {
		this.onSelect = onSelect;
	}

	public String getOnSelect() {
		return onSelect;
	}

	@Override
	public int doStartTag() throws BlingParserException {
		super.doStartTag();

		Composite composite = (Composite) parent.getWidget();
		button = new Button(composite, SWT.PUSH);

		if (!Util.isBlankOrNull(onSelect)) {
			button.addSelectionListener(new EventProcessor(onSelect));
		}

		StyleParser.applyStyle((Control) getWidget(), style);
		
		return EVAL_PAGE;
	}

	@Override
	public int doAfterBody() throws BlingParserException {
		super.doAfterBody();
		button.setText(bodyContent);
		return EVAL_PAGE;
	}

	@Override
	public Widget getWidget() throws BlingParserException {
		return button;
	}

}
